package com.github.thinwonton.spring.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.thinwonton.spring.entity.User;

@Controller
@RequestMapping("/api/user")
public class UserController {

	@RequestMapping(value = { "", "/" }, method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public Object getUser(@RequestParam(value = "callback", required = false) String callback) {
		User user = new User();
		user.setId(100L);
		user.setAge(20);
		user.setName("hugo");
		
		//如果不是JSONP调用，返回正常数据
		if (StringUtils.isBlank(callback)) {
			return user;
		}

		// 返回jsonp格式
		MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(user);
		mappingJacksonValue.setJsonpFunction(callback);
		return mappingJacksonValue;
	}

}
